import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import emailLogin from '../views/emailLogin.vue';
import RegisterView from '../views/RegisterView.vue';
import InfoView from '../views/InfoView.vue'
import HomeView from '../views/HomeView.vue'
import DeviceView from '../views/DeviceView.vue'
import FlowView from '../views/FlowView.vue'

const routes:Array<RouteRecordRaw>=[
  {
    path: "/",
    name: "login",
    component: emailLogin
  },
  {
    path: "/login",
    name: "login2",
    component: ()=>import( "../views/pwdLogin.vue")
  },
  {
    path: "/register",
    name: "register",
    component: RegisterView
  },
  {
    path: "/info",
    name: "info",
    component: InfoView
  },
  {
    path: "/home",
    name: "home",
    component: HomeView
  },
  {
    path: "/device",
    name: "device",
    component: DeviceView
  },
  {
    path: "/flow",
    name: "flow",
    component: FlowView
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes:routes
})
router.beforeEach((to,from,next)=>{
  if(to.matched.some((route)=>route.meta.Auth)){
      if(localStorage.getItem('token')){
          next()
      }else{
           next({
              path:'/',
              query:{
                  returnURL:to.path
              }
          })
      }
     
  }else{
       next()
  }
 
})
export default router
